clear all 
set more off

cd "/Users/yasenov/Dropbox/June 2016/Writeup/Economic Inquiry/EI R and R 2"
use "/Users/yasenov/Dropbox/data_dta/master.dta"
drop if substr(subject, 1, 2) == "it" | substr(subject, 1, 2) == "cs" 

tostring id grade year, replace
gen course = grade + " " + subject + " " + cohort
gen class = year + " " + course
gen studentxclass = id + " " + class
gen gradebook = year + grade + cohort
destring id grade year, replace

bysort month class: egen rank = rank(marks)

gen malelate = male*late_start
gen maleteachermale = male*teacher_male

eststo clear

eststo: xi: qui reg rank i.male, vce(cluster class)
estadd local classfe " "
estadd local monthfe " "
estadd local controls " "  
eststo: xi: qui reg rank i.male malelate maleteachermale teacher_male late_start, vce(cluster class)
estadd local classfe " "
estadd local monthfe " "
estadd local controls " "
eststo: xi: qui reg rank i.male malelate maleteachermale teacher_male late_start i.month, vce(cluster class)
estadd local classfe " "
estadd local monthfe " X "
estadd local controls " "
eststo: xi: qui areg rank i.male malelate maleteachermale teacher_male late_start, absorb(class) vce(cluster class)
estadd local classfe " X "
estadd local monthfe " "
estadd local controls " "
eststo: xi: qui areg rank i.male malelate maleteachermale teacher_male late_start i.month, absorb(class) vce(cluster class)
estadd local classfe " X "
estadd local monthfe " X "
estadd local controls " "

label var _Imale_1 "\ \ \ Male"
label var maleteachermale "\ \ \ Male X \\ \hspace{0.3cm} Male Teacher"
label var malelate "\ \ \ Male X \\ \hspace{0.3cm} Late Start"

esttab using "results-rank2.tex", replace ///
	keep(maleteachermale malelate _Imale_1) se(3) b(3) r2 star(* 0.10 ** 0.05 *** 0.01) nonum nonotes  ///
	noconstant nogaps scalars("classfe Class FE" "monthfe Month FE" "controls Controls") ///
    mtitles("(1)" "(2)" "(3)" "(4)" "(5)") label prehead("") posthead("") 
